package com.uam.core.config;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger2配置
 *
 * @author heguanglong
 * @date 2020/3/23 下午1:15
 */
@Configuration
@EnableSwagger2
@ComponentScan("com.uam.core.controller")
public class Swagger2Config {
    /**
     * 参数配置
     *
     * @author heguanglong
     * @date 2020/3/23 下午4:57
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 项目描述
                .apiInfo(apiInfo()).select()
                // 扫描指定包
                .apis(RequestHandlerSelectors.basePackage("com.uam.core.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any()).build()
                // 指定请求方式的返回描述
                .globalResponseMessage(RequestMethod.GET, getResponseMessages())
                .globalResponseMessage(RequestMethod.POST, getResponseMessages())
                .globalResponseMessage(RequestMethod.PATCH, getResponseMessages())
                .globalResponseMessage(RequestMethod.PUT, getResponseMessages())
                // 添加所有接口的公共参数
                .globalOperationParameters(getPublicParam())
                .globalResponseMessage(RequestMethod.DELETE, getResponseMessages());
    }

    /**
     * 获取接口公共参数
     *
     * @author heguanglong
     * @date 2020/3/23 下午5:07
     */
    private List<Parameter> getPublicParam() {
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder().name("Content-Type").description("请求类型字符串：application/x-www-form" +
                "-urlencoded").defaultValue("application/x-www-form-urlencoded")
                .modelRef(new ModelRef("application/x-www-form-urlencoded")).parameterType("header").required(false).build());
        return parameters;
    }

    /**
     * 0 为请求成功
     * 500 为服务器错误
     * 1 请求失败
     *
     * @author heguanglong
     * @date 2020/3/23 下午4:57
     */
    private List<ResponseMessage> getResponseMessages() {
        List<ResponseMessage> responseMessages = new ArrayList<>();
        return responseMessages;
    }

    /**
     * 项目描述
     *
     * @author heguanglong
     * @date 2020/3/23 下午4:58
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("UAM").description("UAM").version("1.0").build();
    }
}
